﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using Microsoft.Office.Interop.Excel;
using Excel_12 = Microsoft.Office.Interop.Excel;
namespace QuanLyBanHang_N3A
{
    public partial class FormThongTinNhanVien : Form
    {
        public FormThongTinNhanVien()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
        void CapNhatDanhSach()
        {
            List<NhanVienDTO> ds = new List<NhanVienDTO>();
            NhanVienBUS bus = new NhanVienBUS();
            ds = bus.LayThongTinNhanVien();
            dataGridViewThongTinNV.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
                dataGridViewThongTinNV.Rows.Add(i + 1, ds[i].MaNV, ds[i].TenNV, ds[i].GTinh, ds[i].DChi, ds[i].DThoai);
        }
        private void FormThongTinNhanVien_Load(object sender, EventArgs e)
        {
            //List<NhanVienDTO> ds = new List<NhanVienDTO>();
            //NhanVienBUS bus = new NhanVienBUS();
            //ds = bus.LayThongTinNhanVien();
            //dataGridViewThongTinNV.Rows.Clear();
            //for (int i = 0; i <= ds.Count - 1; i++)
            //    dataGridViewThongTinNV.Rows.Add(i+1,ds[i].MaNV,ds[i].TenNV,ds[i].GTinh,ds[i].DChi,ds[i].DThoai);
            CapNhatDanhSach();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FormThemNhanVien frm = new FormThemNhanVien();
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Chưa biết làm !");
            CapNhatDanhSach();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            FormXoaNhanVien frm = new FormXoaNhanVien();
            frm.ShowDialog();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            FormSuaThongTinNhanVien frm = new FormSuaThongTinNhanVien();
            frm.ShowDialog();
        }

        private void btnXuatFile_Click(object sender, EventArgs e)
        {
            try
            {
                ApplicationClass excel = new ApplicationClass();
                excel.Application.Workbooks.Add(true);

                Excel_12.Application oExcel_12 = null;                //Excel_12 Application
                Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
                Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
                Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
                Excel_12.Range oRange = null;                         // Cell or Range in worksheet
                Object oMissing = System.Reflection.Missing.Value;
                oExcel_12 = new Excel_12.Application();
                // Make Excel_12 visible to the user.
                oExcel_12.Visible = true;
                // Set the UserControl property so Excel_12 won't shut down.
                oExcel_12.UserControl = true;
                // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                // Add a workbook.
                oBook = oExcel_12.Workbooks.Add(oMissing);
                // Get worksheets collection
                oSheetsColl = oExcel_12.Worksheets;

                oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

                for (int j = 0; j < dataGridViewThongTinNV.Columns.Count; j++)
                {
                    oRange = (Excel_12.Range)oSheet.Cells[1, j + 1];
                    oRange.Value2 = dataGridViewThongTinNV.Columns[j].HeaderText;
                }

                for (int i = 0; i < dataGridViewThongTinNV.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridViewThongTinNV.Columns.Count; j++)
                    {

                        oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1];
                        oRange.Value2 = dataGridViewThongTinNV.Rows[i].Cells[j].Value;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
